<template>
  <div class="login-page">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" title="登录" left-text="关闭" @click-left="$router.back()"/>
    <!-- 导航栏 -->
    <div class="from-wrap">
    <!-- 表单 -->
    <van-form @submit="onSubmit" ref="loginForm">
      <van-cell-group inset>
        <van-field
          v-model="user.mobile"
          name="mobile"
          label="手机号"
          placeholder="请输入手机号"
          :rules="userFromRules.mobile"
        >
          <template #left-icon>
            <i class="iconfont icon-shouji"></i>
          </template>
        </van-field>
        <van-field
          v-model="user.code"
          name="验证码"
          label="验证码"
          placeholder="请输入验证码"
          :rules="userFromRules.code"
        >
          <template #left-icon>
            <i class="iconfont icon-yanzhengma"></i>
          </template>
            <template #button>
              <van-count-down v-if="isShowCountDown" :time="60*1000"
              format="ss 秒" @finish='isShowCountDown=false'>
                <template #default="timeData">
                  <span class="count-down">{{ timeData.seconds + '秒' }}</span>
                </template>
              </van-count-down>
              <van-button v-else round class="yzm-btn" @click="onSendYzm"
              >发送验证码</van-button>
            </template>
        </van-field>
      </van-cell-group>
      <div style="margin: 16px" class="submit-btn-wrap">
        <van-button class="submit-btn" block type="primary" native-type="submit">
          登录
        </van-button>
      </div>
    </van-form>
    <!-- 表单 -->
    </div>
  </div>
</template>

<script>
import { login, sendYzm } from '@/api/user.js'
export default {
  name: 'LoginPage',
  data () {
    return {
      user: {
        mobile: '',
        code: ''
      },
      userFromRules: {
        mobile: [{
          required: true,
          message: '手机号码不能为空'
        }, {
          pattern: /^1(?:3\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\d|9\d)\d{8}$/,
          message: '请输入正确的手机号'
        }],
        code: [{
          required: true,
          message: '验证码不能为空'
        }, {
          pattern: /^\d{6}/,
          message: '请输入正确的验证码'
        }]
      },
      isShowCountDown: false
    }
  },
  methods: {
    async onSubmit () {
      // 获取表单数据
      const user = this.user
      // 表单验证
      this.$toast.loading({
        duration: 0, // 时间为0，不会消失  但是只要存在新的Toast组件之前的就会撤销
        forbidClick: true,
        message: '登录中'
      })
      // 提交表单数据给后台请求登录
      try {
        const res = await login(user)
        this.$toast.success('登录成功')
        this.$store.commit('saveToken', res)
        this.$router.back()
      } catch (err) {
        // if (err.response.status === 400) {
        //   this.$toast.fail('手机号或验证码有误')
        // } else {
        //   this.$toast.fail('登录失败')
        //   console.log(err)
        // }
        console.log(err)
      }
    },
    // 验证码事件
    async onSendYzm () {
      // 验证手机号
      try {
        await this.$refs.loginForm.validate('mobile')
      } catch (err) {
        return null
      }
      // 点击显示倒计时
      this.isShowCountDown = true
      // 提交给后台发送验证码
      try {
        await sendYzm(this.user.mobile)
        this.$toast('发送成功')
      } catch (err) {
        // 发送失败，立刻关闭计时器
        this.isShowCountDown = false
        if (err.response.status === 429) {
          this.$toast('发送太频繁了，请一分钟后再尝试')
        } else {
          this.$toast('发送失败')
        }
      }
    }
  }
}
</script>

<style  scoped lang='less'>
.login-page {
  .iconfont {
    font-size: 37px;
  }

  .yzm-btn {
    font-size: 22px;
    width: 186px;
    height: 50px;
    line-height: 50px;
    color: #666;
    background-color: #ededed;
  }

  .submit-btn-wrap {
    .submit-btn {
      background-color: rgb(50, 133, 211);
      border-radius: 25px;
      font-size: 30px;
    }
  }

  .from-wrap {
    margin-top: 300px;
    .count-down {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 180px;
      height: 50px;
      background-color: powderblue;
      border-radius: 10px;
    }
  }
}
</style>
